﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarbonMeter
{
    public partial class NakliyeTuketimleri : Form
    {
        //Ert 22.06: maximize eklendi
        public NakliyeTuketimleri()
        {
            InitializeComponent();
            //this.Dock = DockStyle.Fill;
            //this.WindowState = FormWindowState.Maximized;
        }

        //Ert 22.06: maximize eklendi
        private void NakliyeTuketimleri_Load(object sender, EventArgs e)
        {

            comboBox1.DisplayMember = "productName";
            comboBox1.ValueMember = "productId";
            comboBox1.DataSource = Database.getInstance().getProductsFreight(0);// 1--> Kara Tasitlari
            comboBox1.SelectedIndex = 0;

            comboBox2.DisplayMember = "productName";
            comboBox2.ValueMember = "productId";
            comboBox2.DataSource = Database.getInstance().getProductsFreight(1);// 1--> Hava Tasitlari
            comboBox2.SelectedIndex = 0;

            comboBox3.ValueMember = "productId";
            comboBox3.DisplayMember = "productName";
            comboBox3.DataSource = Database.getInstance().getProductsFreight(2);// 1--> Deniz Tasitlari
            comboBox3.SelectedIndex = 0;

            comboBox4.DisplayMember = "productName";
            comboBox4.ValueMember = "productId";
            comboBox4.DataSource = Database.getInstance().getProductsFreight(3);// 1--> Nehir Tasitlari
            comboBox4.SelectedIndex = 0;

            //datagridview'e data source baglama islemi
            customDataGridView1.DataSource = Database.getInstance().getRoadFreightConsumptionByUserAndTime(MainForm.user.userId, DateTime.Now);
            customDataGridView2.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 1, DateTime.Now);
            customDataGridView3.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 2, DateTime.Now);
            customDataGridView4.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 3, DateTime.Now);

            button2.Text = "KAYDET";
            button6.Text = "KAYDET";
            button10.Text = "KAYDET";
            button14.Text = "KAYDET";

            button2.Enabled = false;
            button6.Enabled = false;
            button10.Enabled = false;
            button14.Enabled = false;

            this.BringToFront();
            //this.Dock = DockStyle.Fill;
            this.WindowState = FormWindowState.Maximized;
        }

        private void cleanFormKara()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox14.Text = "";
            textBox13.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)  //Temizle Kara
        {
            cleanFormKara();
        }

        private void button1_Click(object sender, EventArgs e)  //Hesapla Kara
        {
            try
            {
                float distance = float.Parse(textBox2.Text);
                //TODO: seyahata orn 100den kucuk olmasi kontrol
                float bosOrani = float.Parse(textBox1.Text);
                float bosSeyahat = (float)(distance * bosOrani / 100);
                float doluSeyahat = distance - bosSeyahat;
                float CsalinimMiktari = (bosSeyahat * bosSalinimMiktari + doluSeyahat * yukluSalinimMiktari); 

                textBox14.Text = "" + Math.Round((double)CsalinimMiktari,2);
                textBox13.Text = "" + Math.Round((double)(CsalinimMiktari * 44 / 12),2);

                button2.Enabled = true;
                button2.Text = "KAYDET";
            }
            catch (Exception)
            {
                MessageBox.Show("Kulllanım miktarını düzgün giriniz.");
            }
        }

        RoadFreightConstant roadFreightConst;
        float bosSalinimMiktari, yukluSalinimMiktari;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) //Urun sec Kara
        {
            if (comboBox1.SelectedValue == null) return;
            int productId = (int)comboBox1.SelectedValue;
            roadFreightConst = Database.getInstance().getRoadFreightConstant(productId);

            bosSalinimMiktari = (float)roadFreightConst.kgCEqKmUnloadedTruck;
            yukluSalinimMiktari = (float)roadFreightConst.kgCEqKmVehicleFull;
        }

        //TODO: roadFreightConstant'dan percentageUnload ve percentageCUMaxLoad cikarilarak bunlar product name ile birlestirilebilir
        private void button2_Click(object sender, EventArgs e)  //Kara Kayıt
        {
            try
            {
                if (button2.Text.Equals("KAYDET"))
                    Database.getInstance().addRoadFreightConsumption(roadFreightConst.productId, float.Parse(textBox1.Text), float.Parse(textBox2.Text)
                    , float.Parse(textBox14.Text), dateTimePicker1.Value,textBox18.Text);
                else
                {
                    button1_Click(sender, e);
                    int recordId = (int)customDataGridView1.SelectedRows[0].Cells["recordId"].Value;
                    Database.getInstance().updateRoadFreightConsumption(recordId, roadFreightConst.productId, float.Parse(textBox1.Text), float.Parse(textBox2.Text),
                    float.Parse(textBox14.Text), dateTimePicker1.Value, textBox18.Text
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            cleanFormKara();
            customDataGridView1.DataSource = Database.getInstance().getRoadFreightConsumptionByUserAndTime(MainForm.user.userId, DateTime.Now);
            button2.Text = "KAYDET";
        }

        //Not: doluluk orani eklendi
        private void button4_Click(object sender, EventArgs e)  //Değiştir Kara butonu
        {
            try
            {
                DataGridViewRow row = customDataGridView1.SelectedRows[0];

                comboBox1.SelectedValue = row.Cells["productId"].Value;
                textBox2.Text = row.Cells["distanceKm"].Value.ToString();
                textBox1.Text = row.Cells["fillingRatio"].Value.ToString();
                textBox14.Text = "" + Math.Round(Double.Parse(row.Cells["carbonAmount"].Value.ToString()), 2);
                button1_Click(sender, e);
                dateTimePicker1.Value = (DateTime)row.Cells["billDate"].Value;
                textBox18.Text = row.Cells["plate"].Value.ToString();
            }
            catch (Exception myEx)
            {
                MessageBox.Show(myEx.StackTrace);
            }
            
            button2.Text = "GÜNCELLE";
            button2.Enabled = true;
        }
        ////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////HAVA//////////////////////////////////////
        //TODO: kyoto dahili ve harici olarak ayriyeten gosterilebilir textbox ile
        FreightConstant freightConst;
        float kyotoDahiliKatsayisi, kyotoHariciKaysayisi;
        int grupNoHava;
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) //Urun hava
        {
            if (comboBox2.SelectedValue.GetType() != typeof(int))
                return;
            int productId = (int)comboBox2.SelectedValue;   //dikkat id valueda tutuluyor
            freightConst = Database.getInstance().getFreightConstant(productId);
            grupNoHava = Database.getInstance().getGroupId(productId);

            kyotoDahiliKatsayisi = (float)freightConst.kgCEqPerTonKm;
            kyotoHariciKaysayisi = (float)freightConst.kgCEqPerTonKmNotIncKyoto;
        }

        private void button5_Click(object sender, EventArgs e)  //Hesapla Hava
        {
            try
            {
                float distance = float.Parse(textBox8.Text);
                float yukMiktari = float.Parse(textBox9.Text);
                bool kyotoKapsaminda = checkBox3.Checked;
                float CsalinimMiktari = 0;
                if (kyotoKapsaminda)
                {
                    CsalinimMiktari = kyotoDahiliKatsayisi * distance * yukMiktari;
                }
                else
                {
                    CsalinimMiktari = kyotoHariciKaysayisi * distance * yukMiktari;
                }
                textBox7.Text = "" + Math.Round((double)CsalinimMiktari,2);
                textBox3.Text = "" + Math.Round((double)(CsalinimMiktari * 44 / 12),2);

                button6.Enabled = true;
                button6.Text = "KAYDET";
            }
            catch (Exception)
            {
                MessageBox.Show("Kulllanım miktarını düzgün giriniz.");
            }
        }

        private void cleanFormHava()
        {
            textBox3.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            checkBox3.Checked = false;
        }

        private void button6_Click(object sender, EventArgs e)  //Kaydet Hava 
        {
            if (button6.Text.Equals("KAYDET"))
                Database.getInstance().addFreightConsumption(freightConst.productId, float.Parse(textBox9.Text), -1, -1
                    , float.Parse(textBox8.Text), (checkBox3.Checked ? 1 : 0), float.Parse(textBox7.Text), dateTimePicker2.Value, textBox19.Text);
            else//Guncelle
            {
                button5_Click(sender, e);
                int recordId = (int)customDataGridView2.SelectedRows[0].Cells["recordIdHava"].Value;
                Database.getInstance().updateFreightConsumption(recordId, freightConst.productId, float.Parse(textBox9.Text), -1, -1,
                    float.Parse(textBox8.Text), (checkBox3.Checked ? 1 : 0), float.Parse(textBox7.Text), dateTimePicker2.Value,textBox19.Text
                );
            }

            cleanFormHava();
            customDataGridView2.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 1, DateTime.Now);
            button6.Text = "KAYDET";
        }

        private void button7_Click(object sender, EventArgs e)  //Temizle Hava
        {
            cleanFormHava();
        }

        private void button8_Click(object sender, EventArgs e)  //Değiştir Hava
        {
            DataGridViewRow row = customDataGridView2.SelectedRows[0];
            //NOT: Burada row da kullanilan name'ler design nameleri
            comboBox2.SelectedValue = row.Cells["productIdHava"].Value;
            textBox8.Text = row.Cells["transportationDistanceHava"].Value.ToString();
            textBox9.Text = row.Cells["amountKgHava"].Value.ToString();
            checkBox3.Checked = (bool)row.Cells["inKyotoHava"].Value;
            //textBox14.Text = row.Cells["carbonAmountHava"].Value.ToString();
            button5_Click(sender, e);
            dateTimePicker2.Value = (DateTime)row.Cells["billDateHava"].Value;
            textBox19.Text = row.Cells["plateHava"].Value.ToString();

            button6.Text = "GÜNCELLE";
            button6.Enabled = true;
        }
        ////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////DENIZ/////////////////////////////////////
        //TODO: all ships: 114'e gore degisiklikler yapilmali, grup no: 14

        FreightConstant freightConstDeniz;
        float katsayiTonsOfFuelDay, katsayiTonsPerKm;
        int grupNo;
        private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox3.SelectedValue == null) return;
            int productId = (int)comboBox3.SelectedValue;   //dikkat id valueda tutuluyor
            freightConstDeniz = Database.getInstance().getFreightConstant(productId);
            grupNo = Database.getInstance().getGroupId(productId);

            //TODO: tons of fuel day carbon katsayisi ile carpilip karbon salinimi bulunmali
            if (freightConstDeniz.tonsOfFuelPerDay != null)
                katsayiTonsOfFuelDay = (float)freightConstDeniz.tonsOfFuelPerDay;
            if (freightConstDeniz.kgCEqPerTonKm != null)
                katsayiTonsPerKm = (float)freightConstDeniz.kgCEqPerTonKm;
            if (grupNo == 14)     //NOT: sadece all ships secilmisse mesafe girme olacak
            {
                textBox17.Enabled = true;
                textBox11.Enabled = false;  //seyahat suresi
                textBox12.Enabled = true;
            }
            else
            {
                textBox11.Enabled = true;
                textBox17.Enabled = false;
                textBox12.Enabled = false;
            }
            //TODO: capacityM3, tonsCarried,maxTonShipped icin sadece okunabilir text konulabilir
        }

        private void button9_Click(object sender, EventArgs e)  //Hesapla Deniz
        {
            try
            {
                float distance = 0;
                float yukMiktari = 0;
                float seyahatSuresi = 0;
                if (grupNo == 14)
                {
                    distance = float.Parse(textBox12.Text);
                    yukMiktari = float.Parse(textBox17.Text);
                }
                else
                    seyahatSuresi = float.Parse(textBox11.Text);


                //TODO: all ships in grup numarasi dogru girilmeli
                float CsalinimMiktari = 0;
                if (grupNo == 14)             //All ships e esitse
                    CsalinimMiktari = katsayiTonsPerKm * yukMiktari * distance;
                else
                    CsalinimMiktari = katsayiTonsOfFuelDay * seyahatSuresi;

                textBox10.Text = "" + Math.Round((double)CsalinimMiktari,2);
                textBox4.Text = "" + Math.Round((double)(CsalinimMiktari * 44 / 12),2);

                button10.Enabled = true;
                button10.Text = "KAYDET";
            }
            catch (Exception E)
            {
                MessageBox.Show(E.StackTrace);
                MessageBox.Show("Kulllanım miktarını düzgün giriniz.");
            }
        }

        private void cleanFormDeniz()
        {
            textBox11.Text = "";
            textBox12.Text = "";
            textBox17.Text = "";
            textBox10.Text = "";
            textBox4.Text = "";
        }

        private void button10_Click(object sender, EventArgs e) //Kaydet Deniz
        {
            try
            {   //TODO: kaydette burada duzenleme yapilmali
                //TODO: grupNo all ships icin duzenlenmeli
                if (button10.Text.Equals("KAYDET"))
                    Database.getInstance().addFreightConsumption(freightConstDeniz.productId, (grupNo == 14 ? float.Parse(textBox17.Text) : -1), -1, (grupNo == 14 ? -1 : float.Parse(textBox11.Text))
                    , (grupNo == 14 ? float.Parse(textBox12.Text) : -1), -1, float.Parse(textBox10.Text), dateTimePicker3.Value, textBox20.Text);
                else//Guncelle
                {
                    button9_Click(sender, e);
                    int recordId = (int)customDataGridView3.SelectedRows[0].Cells["recordIdDeniz"].Value;
                    Database.getInstance().updateFreightConsumption(recordId, freightConstDeniz.productId, (grupNo == 14 ? float.Parse(textBox17.Text) : -1), -1, (grupNo == 14 ? -1 : float.Parse(textBox11.Text))
                    , (grupNo == 14 ? float.Parse(textBox12.Text) : -1), -1, float.Parse(textBox10.Text), dateTimePicker3.Value, textBox20.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            cleanFormDeniz();
            customDataGridView3.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 2, DateTime.Now);  //2: deniz
            button6.Text = "KAYDET";
        }

        private void button11_Click(object sender, EventArgs e) //temizle Deniz
        {
            cleanFormDeniz();
        }

        private void button12_Click(object sender, EventArgs e) //değiştir deniz
        {
            try
            {
                DataGridViewRow row = customDataGridView3.SelectedRows[0];
                //
                comboBox3.SelectedValue = row.Cells["productIdDeniz"].Value;
                grupNo = Database.getInstance().getGroupId(int.Parse(row.Cells["productIdDeniz"].Value.ToString()));

                if (grupNo == 14)   //all ships ise
                {
                    textBox12.Text = row.Cells["transportationDistanceDeniz"].Value.ToString();
                    textBox17.Text = row.Cells["amountKgDeniz"].Value.ToString();
                }
                else
                {
                    textBox11.Text = row.Cells["transportationDaysDeniz"].Value.ToString();
                }

                //textBox14.Text = row.Cells["carbonAmountDeniz"].Value.ToString();
                button9_Click(sender, e);
                dateTimePicker3.Value = (DateTime)row.Cells["billDateDeniz"].Value;
                textBox20.Text = row.Cells["plateDeniz"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show(ex.StackTrace);
            }

            button10.Text = "GÜNCELLE";
            button10.Enabled = true;
        }
        ////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////NEHIR/////////////////////////////////////
        FreightConstant freightConstNehir;
        float katsayiTonsPerKmNehir;
        private void comboBox4_SelectedValueChanged(object sender, EventArgs e)
        {
            //TODO: form load ederken rastlanan bu hata duzeltilmeli
            if (comboBox4.SelectedValue == null) return;
            int productId = (int)comboBox4.SelectedValue;   //dikkat id valueda tutuluyor
            freightConstNehir = Database.getInstance().getFreightConstant(productId);

            katsayiTonsPerKmNehir = (float)freightConstNehir.kgCEqPerTonKm;
        }

        private void button13_Click(object sender, EventArgs e) //hesapla Nehir
        {
            try
            {
                float distance = float.Parse(textBox16.Text);
                float yukMiktari = float.Parse(textBox15.Text);

                float CsalinimMiktari = katsayiTonsPerKmNehir * yukMiktari * distance;

                textBox6.Text = "" + Math.Round((double)CsalinimMiktari,2);
                textBox5.Text = "" + Math.Round((double)(CsalinimMiktari * 44 / 12),2);

                button14.Enabled = true;
                button14.Text = "KAYDET";
            }
            catch (Exception)
            {
                MessageBox.Show("Kulllanım miktarını düzgün giriniz.");
            }
        }

        private void cleanFormNehir()
        {
            textBox16.Text = "";
            textBox15.Text = "";
            textBox6.Text = "";
            textBox5.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)     //Kaydet Nehir
        {
            try
            {
                if (button14.Text.Equals("KAYDET"))
                    Database.getInstance().addFreightConsumption(freightConstNehir.productId, float.Parse(textBox15.Text), -1, -1
                    , float.Parse(textBox16.Text), -1, float.Parse(textBox6.Text), dateTimePicker4.Value, textBox21.Text);
                else//Guncelle
                {
                    button13_Click(sender, e);
                    int recordId = (int)customDataGridView4.SelectedRows[0].Cells["recordIdNehir"].Value;
                    Database.getInstance().updateFreightConsumption(recordId, freightConstNehir.productId, float.Parse(textBox15.Text), -1, -1
                    , float.Parse(textBox16.Text), -1, float.Parse(textBox6.Text), dateTimePicker4.Value, textBox21.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                MessageBox.Show(ex.Message);
                return;
            }

            cleanFormNehir();
            customDataGridView4.DataSource = Database.getInstance().getFreightConsumptionByUserAndTime(MainForm.user.userId, 3, DateTime.Now);  //3: Nehir grubu
            button14.Text = "KAYDET";
        }

        private void button15_Click(object sender, EventArgs e) //temizle Nehir
        {
            cleanFormNehir();
        }

        private void button16_Click(object sender, EventArgs e) //Degistir Nehir
        {
            DataGridViewRow row = customDataGridView4.SelectedRows[0];
            //
            comboBox4.SelectedValue = row.Cells["productIdNehir"].Value;
            textBox16.Text = row.Cells["transportationDistanceNehir"].Value.ToString();
            textBox15.Text = row.Cells["amountKgNehir"].Value.ToString();
            //textBox14.Text = row.Cells["carbonAmount"].Value.ToString();
            button13_Click(sender, e);  //hesapla
            dateTimePicker4.Value = (DateTime)row.Cells["billDateNehir"].Value;
            textBox21.Text = row.Cells["plateNehir"].Value.ToString();

            button14.Text = "GÜNCELLE";
            button14.Enabled = true;
        }



    }
}